package com.atguigu.gulimall.product.web;



import com.atguigu.gulimall.product.config.MyRedissonConfig;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-06-08 14:14
 **/
// 这里要注意 RestController 和 Controller注解的区别
//@RestController
@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping(value = {"/","index.html"})
    private String indexPage(Model model) {
        //1、查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();

        return catalogJson;

    }

    @GetMapping(value = "lockTest")
    @ResponseBody
    public String lockTest() {
        //1.获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my_lock");
        try {
            // 2. 加锁
            lock.lock();  // 阻塞式等待，默认加的锁都是30s时间
            //1）、锁的自动续期，如果业务超长，运行期间自动锁上新的30s。不用担心业务时间长，锁自动过期被删掉
            //2）、加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认会在30s内自动过期，不会产生死锁问题
            // myLock.lock(10,TimeUnit.SECONDS);   //10秒钟自动解锁,自动解锁时间一定要大于业务执行时间
            //问题：在锁时间到了以后，不会自动续期
            //1、如果我们传递了锁的超时时间，就发送给redis执行脚本，进行占锁，默认超时就是 我们制定的时间
            //2、如果我们指定锁的超时时间，就使用 lockWatchdogTimeout = 30 * 1000 【看门狗默认时间】
            //只要占锁成功，就会启动一个定时任务【重新给锁设置过期时间，新的过期时间就是看门狗的默认时间】,每隔10秒都会自动的再次续期，续成30秒
            // internalLockLeaseTime 【看门狗时间】 / 3， 10s

            // 最佳实践
           // myLock.lock(30,TimeUnit.SECONDS);省掉了整个续期操作
            System.out.println("加锁成功,执行业务。。。。。"+ Thread.currentThread().getId());
            Thread.sleep(30000);
        }catch (Exception e) {

        }finally {
            // 3.解锁  思考：假如解锁代码没有运行，redisson会不会造出死锁？？
            System.out.println("释放锁。。。。。"+ Thread.currentThread().getId());
            lock.unlock();
        }
        return "lockTest" + Thread.currentThread().getId();
    }



    /**
     * 保证一定能读到最新数据，修改期间，写锁是一个排他锁（互斥锁）。读锁是一个共享锁
     * 写锁没释放，读就必须等待
     *
     * * 读 + 读 ：相当于无锁，并发读，只会在redis中记录好，所有当前的读锁，他们都会同时加锁成功
     *  * 写 + 读 ：等待写锁释放
     *  * 写 + 写：阻塞方式
     *  * 读 + 写：有读锁，写也需要等待
     *  * //只要有写的存在，都必须等待
     * @return
     */
    @GetMapping("/read")
    @ResponseBody
    public String readValue() {

        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = readWriteLock.readLock();
        try {
            rLock.lock();
            s = stringRedisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }


    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {

        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = readWriteLock.writeLock();
        try {
            //1、改数据加写锁，读数据加读锁
            rLock.lock();
            Thread.sleep(30000);
            s = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("writeValue", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }





    /**
     * 车库停车
     * 3车位
     * 信号量也可以做分布式限流
     */
    @GetMapping(value = "/park")
    @ResponseBody
    public String park() throws InterruptedException {

        RSemaphore park = redisson.getSemaphore("park");
        // park.acquire();     //获取一个信号、获取一个值,占一个车位  减法   阻塞
        boolean flag = park.tryAcquire();   // 尝试 不阻塞

        if (flag) {
            //执行业务
        } else {
            return "error";
        }

        return "ok=>" + flag;
    }

    @GetMapping(value = "/go")
    @ResponseBody
    public String go() {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();     //释放一个车位   加法
        return "ok";
    }




    /**
     * 放假、锁门
     * 1班没人了
     * 5个班，全部走完，我们才可以锁大门
     * 分布式闭锁
     */

    @GetMapping(value = "/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {

        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();       //等待闭锁完成

        return "放假了...";
    }

    @GetMapping(value = "/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id) {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();       //计数-1

        return id + "班的人都走了...";
    }


    @GetMapping(value = "/getCate/{id}")
    @ResponseBody
    public CategoryEntity test (@PathVariable("id") Long id) {
        CategoryEntity categoryEntity = categoryService.getCategoryById(id);
        return categoryEntity;
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TestList  {
        private Integer id;
        private String name;
    }


    // list转变成map方法
    @GetMapping(value = "/test")
    @ResponseBody
    public Map<Integer, TestList> myTest(){
        List<TestList> list = new ArrayList<>();
        list.add(new TestList(1, "张三"));
        list.add(new TestList(2, "李四"));
        Map<Integer, TestList> collect = list.stream().collect(Collectors.toMap(k -> k.getId(), v -> v));
        collect.put(3, null);
       return  collect;
    }
}
